using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Anthem;
using Common;
using eWorld.UI;
using CompareValidator = Anthem.CompareValidator;
using CustomValidator = Anthem.CustomValidator;
using RangeValidator = Anthem.RangeValidator;
using RegularExpressionValidator = Anthem.RegularExpressionValidator;
using RequiredFieldValidator = Anthem.RequiredFieldValidator;

namespace MinCultura.Sinic.WebUserControls
{
	/// <summary>
	///		Summary description for Cuidades.
	/// </summary>
	public class SinicUserControl : UserControl
	{
		#region Properties

		public bool EnabledAllControls
		{
			set{EnableControls(value);}
		}

		public bool EnabledValidator
		{
			set{EnableValidatorControls(value);}
		}

		public string BasePath
		{
			get
			{
				if (Page.Application[Utilidad._APPLICATION_BASE_PATH] != null)
				{
					return Page.Application[Utilidad._APPLICATION_BASE_PATH].ToString();
				}
				else
				{
					return String.Empty;
				}
			}			
		}

		public string PageName
		{
			get
			{
				if (ViewState["PageName"]==null)
				{
					int nIndex=0;
					string pageName="";
					string path=this.Page.Request.Url.AbsolutePath;
					string queryString = this.GetQueryString();

					// Strip The Page Name
					nIndex = path.LastIndexOf("/");
					if (nIndex>0)
					{
						pageName = path.Substring(nIndex+1,(path.Length-nIndex)-1);
						path = path.Substring(0,nIndex);
					}

					if (!string.IsNullOrEmpty(queryString))
					{
						pageName += queryString;
					}

					return pageName;
				}
				else
				{
					return ViewState["PageName"].ToString();
				}
			}
			set
			{
				ViewState["PageName"] = value;
			}
		}


		// Permite almacenar los mensajes relacionados al user control.
		public string Mensaje
		{
			get
			{
				if (ViewState["Mensaje"] != null)
					return ViewState["Mensaje"].ToString();
				else
					return String.Empty;
			}
			set { ViewState["Mensaje"] = value; }
		}

		#endregion

		private void Page_Load(object sender, EventArgs e)
		{
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		private string GetQueryString()
		{
			string queryString = "?";
			bool loaded = false;

			if (Request.QueryString["idFuncion"] != null && Utilidad.IsNumeric(Request.QueryString["idFuncion"]))
			{
				if (queryString.Length>1)
				{
					queryString += "&idFuncion=" + Request.QueryString["idFuncion"];
				}
				else
				{
					queryString += "idFuncion=" + Request.QueryString["idFuncion"];
				}
				
				loaded = true;
			}
			if (!loaded)
				queryString = String.Empty;

			return queryString;
		}

		protected void EnableValidatorControls(bool habilitar)
		{
			foreach (Control oControl in this.Controls)
			{
				switch (oControl.GetType().ToString())
				{
					case "Anthem.RequiredFieldValidator":
				        RequiredFieldValidator reqDomValidator = (RequiredFieldValidator)oControl;

						if (reqDomValidator!=null)							
								reqDomValidator.Enabled =  habilitar;
						break;

					case "Anthem.RegularExpressionValidator":
				        RegularExpressionValidator regValidator = (RegularExpressionValidator)oControl;

						if (regValidator!=null)							
								regValidator.Enabled = habilitar;
						break;

					case "Anthem.CustomValidator":
				        CustomValidator cusValidator = (CustomValidator)oControl;

						if (cusValidator!=null)							
								cusValidator.Enabled = habilitar;
						break;

					case "Anthem.RangeValidator":
				        RangeValidator ranValidator = (RangeValidator)oControl;

						if (ranValidator!=null)							
							ranValidator.Enabled = habilitar;
						break;

					case "Anthem.CompareValidator":
				        CompareValidator comValidator = (CompareValidator)oControl;

						if (comValidator!=null)							
							comValidator.Enabled = habilitar;
						break;
				}
			}
		}

		protected void EnableControls(bool habilitar)
		{
			foreach (Control oControl in this.Controls)
			{
				switch (oControl.GetType().ToString())
				{
					case  "Anthem.DataGrid":
					case "UsrControls.DataGridBase":
					case "UsrControls.DataGridBaseBasica":
					case "UsrControls.DataGridBaseBasicaLibre":
				        DataGrid objDataGrid = (DataGrid)oControl;

						if (objDataGrid!=null)
							objDataGrid.Enabled = habilitar;
						break;
						
					case  "Anthem.TextBox":
					case  "UsrControls.TextBoxBase":
				        TextBox objTextBox = (TextBox)oControl;

						if (objTextBox!=null)
							objTextBox.Enabled = habilitar;
						break;
					case  "System.Web.UI.HtmlControls.HtmlInputText":
				        HtmlInputText htmlInputText = (HtmlInputText)oControl;

						if (htmlInputText!=null)
							htmlInputText.Disabled = !habilitar;
						break;
					case  "System.Web.UI.HtmlControls.HtmlTextArea":
				        HtmlTextArea htmlTextArea = (HtmlTextArea)oControl;

						if (htmlTextArea!=null)
							htmlTextArea.Disabled = !habilitar;
						break;

					case  "Anthem.CheckBox":
				        CheckBox objCheckBox = (CheckBox)oControl;

						if (objCheckBox != null)
							objCheckBox.Enabled = habilitar;
						break;
					case "Anthem.ListBox":
					case "UsrControls.ListBoxBase":
				        ListBox objListBox = (ListBox)oControl;

						if (objListBox != null)
							objListBox.Enabled = habilitar;
						break;
					case "Anthem.DropDownList":
					case "UsrControls.DropDownBase":

				        DropDownList objDropDownList = (DropDownList)oControl;

						if (objDropDownList != null)
							objDropDownList.Enabled = habilitar;
						break;
					case  "eWorld.UI.NumericBox":
				        NumericBox objNumericTextBox = (eWorld.UI.NumericBox)oControl;

						if (objNumericTextBox!=null)
							objNumericTextBox.Enabled = habilitar;
						break;
					case  "eWorld.UI.TimePicker":
				        TimePicker objTimePicker = (eWorld.UI.TimePicker)oControl;

						if (objTimePicker!=null)
							objTimePicker.Enabled = habilitar;
						break;
					case "Anthem.RequiredFieldValidator":
				        RequiredFieldValidator reqDomValidator = (RequiredFieldValidator)oControl;

						if (reqDomValidator!=null)
							reqDomValidator.Enabled =  habilitar;
						break;
					case "Anthem.RegularExpressionValidator":

				        RegularExpressionValidator regValidator = (RegularExpressionValidator)oControl;

						if (regValidator!=null)
							regValidator.Enabled = habilitar;
						break;

					case "Anthem.CustomValidator":
				        CustomValidator cusValidator = (CustomValidator)oControl;

						if (cusValidator!=null)
							cusValidator.Enabled = habilitar;
						break;

					case "Anthem.RangeValidator":
				        RangeValidator ranValidator = (RangeValidator)oControl;

						if (ranValidator!=null)							
							ranValidator.Enabled = habilitar;
						break;
				}
			}
		}

		public void ClearControls()
		{
			foreach (Control oControl in this.Controls)
			{
				switch (oControl.GetType().ToString())
				{					
					case  "Anthem.TextBox":
					case  "UsrControls.TextBoxBase":
				        TextBox objTextBox = (TextBox)oControl;

						if (objTextBox!=null)
							objTextBox.Text = string.Empty;
						break;

					case  "System.Web.UI.HtmlControls.HtmlInputText":
				        HtmlInputText htmlInputText = (HtmlInputText)oControl;

						if (htmlInputText!=null)
							htmlInputText.Value = string.Empty;
						break;

					case  "System.Web.UI.HtmlControls.HtmlTextArea":
				        HtmlTextArea htmlTextArea = (HtmlTextArea)oControl;

						if (htmlTextArea!=null)
							htmlTextArea.Value = string.Empty;
						break;

					case  "Anthem.CheckBox":
				        CheckBox objCheckBox = (CheckBox)oControl;

						if (objCheckBox != null)
							objCheckBox.Checked = false;
						break;

					case "Anthem.ListBox":
					case "UsrControls.ListBoxBase":
				        ListBox objListBox = (ListBox)oControl;

						if (objListBox != null)
							objListBox.ClearSelection();
						break;

					case "Anthem.CheckBoxList":
				        CheckBoxList objCheckBoxList = (CheckBoxList)oControl;

						if (objCheckBoxList != null)
							objCheckBoxList.ClearSelection();
						break;

					case  "eWorld.UI.NumericBox":
				        NumericBox objNumericTextBox = (eWorld.UI.NumericBox)oControl;

						if (objNumericTextBox!=null)
							objNumericTextBox.Text = string.Empty;
						break;

					case  "eWorld.UI.TimePicker":
				        TimePicker objTimePicker = (eWorld.UI.TimePicker)oControl;

						if (objTimePicker!=null)
							objTimePicker.Clear();
						break;
				}
			}
		}

		public objUsuario Usuario
		{
			get
			{ 
				objUsuario tObjUsuario = new objUsuario();
				if (Session[Utilidad._SESSION_USUARIO]!=null)
				{
					tObjUsuario = (objUsuario)Session[Utilidad._SESSION_USUARIO];				
				}
				return tObjUsuario;
			}
		}

	}
}
